﻿

'---------------------------------------------------------------------
' THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'---------------------------------------------------------------------
Imports System.Drawing
Imports System.ComponentModel

Public Class DataGridViewProgressColumn
    Inherits DataGridViewImageColumn
    Public Sub New()
        Me.CellTemplate = New DataGridViewProgressCell
    End Sub
End Class

Public Class DataGridViewProgressCell
    Inherits DataGridViewImageCell

    'Private _ProgressBar As New ProgressBar()
    'Private _bmp As New Bitmap(_ProgressBar.Width, _ProgressBar.Height)
    Private _strFormat As StringFormat = New StringFormat()

    Sub New()
        ValueType = Type.GetType("Integer")
        _strFormat.Alignment = StringAlignment.Center
        _strFormat.LineAlignment = StringAlignment.Center
    End Sub
    ' Method required to make the Progress Cell consistent with the default Image Cell. 
    ' The default Image Cell assumes an Image as a value, although the value of the Progress Cell is an Integer.
    Protected Overrides Function GetFormattedValue( _
        ByVal value As Object, _
        ByVal rowIndex As Integer, _
        ByRef cellStyle As DataGridViewCellStyle, _
        ByVal valueTypeConverter As TypeConverter, _
        ByVal formattedValueTypeConverter As TypeConverter, _
        ByVal context As DataGridViewDataErrorContexts _
        ) As Object
        Static emptyImage As Bitmap = New Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        GetFormattedValue = emptyImage
    End Function


    Protected Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
        Dim progressVal As Integer = CType(value, Integer)
        Dim percentage As Single = CType((progressVal / 100), Single)
        Dim backBrush As Brush = New SolidBrush(cellStyle.BackColor)
        Dim foreBrush As Brush = New SolidBrush(cellStyle.ForeColor) 'cellStyle.ForeColor

        ' Call the base class method to paint the default cell appearance.
        MyBase.Paint(g, clipBounds, cellBounds, rowIndex, cellState, _
            value, formattedValue, errorText, cellStyle, _
            advancedBorderStyle, paintParts)

        'Const margin As Integer = 2

        '_ProgressBar.Size = New Size(cellBounds.Width - (margin * 2), cellBounds.Height - (margin * 2))
        '_ProgressBar.Value = progressVal
        'Me.ToolTipText = progressVal

        '_ProgressBar.DrawToBitmap(_bmp, _ProgressBar.ClientRectangle)

        'g.DrawImage(_bmp, New Point(cellBounds.X + margin, cellBounds.Y + margin))
        'g.DrawString(progressVal.ToString() & "%", cellStyle.Font, foreBrush, cellBounds.X + (cellBounds.Width / 2) - 4, cellBounds.Y + 4)

        Dim rectDraw As Rectangle = New Rectangle(cellBounds.X + 1, cellBounds.Y + 1, cellBounds.Width - 3, cellBounds.Height - 3)
        Dim rectFill As Rectangle = New Rectangle(cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4)
        Dim rectString As RectangleF = New RectangleF(cellBounds.X + 2, cellBounds.Y + 2, cellBounds.Width - 4, cellBounds.Height - 4)

        g.DrawRectangle(Pens.Black, rectDraw)
        g.FillRectangle(New SolidBrush(Color.YellowGreen), rectFill)
        g.DrawString(progressVal.ToString() & "%", cellStyle.Font, foreBrush, rectString, _strFormat)
        Me.ToolTipText = String.Format("进度: {0}%", progressVal)

        'If percentage > 0.0 Then
        '    ' Draw the progress bar and the text; Color.FromArgb(163, 189, 242)
        '    g.DrawRectangle(Pens.Black, rectDraw)
        '    g.FillRectangle(New SolidBrush(Color.YellowGreen), rectFill)
        '    g.DrawString(progressVal.ToString() & "%", cellStyle.Font, foreBrush, rectString, _strFormat)
        'Else
        '    'draw the text
        '    If Not Me.DataGridView.CurrentCell Is Nothing AndAlso Me.DataGridView.CurrentCell.RowIndex = rowIndex Then
        '        g.DrawRectangle(Pens.Black, rectDraw)
        '        g.DrawString(progressVal.ToString() & "%", cellStyle.Font, New SolidBrush(cellStyle.SelectionForeColor), rectString, _strFormat)
        '    Else
        '        g.DrawRectangle(Pens.Black, rectDraw)
        '        g.DrawString(progressVal.ToString() & "%", cellStyle.Font, foreBrush, rectString, _strFormat)
        '    End If
        'End If
    End Sub
End Class

